/*
 * @Author: sean_kei@163.com
 * @Date: 2021-06-08 15:07:38
 * @LastEditors: sean_kei@163.com
 * @LastEditTime: 2022-08-03 09:48:12
 */
package com.wxhandle.cleandemo.core.domain.identity;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import com.wxhandle.cleandemo.core.domain.base.Auditable;

import org.springframework.context.annotation.Lazy;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Entity
@Table(name = "SysUser")
public class SysUser extends Auditable<String> implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "name", length = 50, nullable = false, unique = true)
    private String name;

    @Column(name = "pwd", length = 100, nullable = false)
    private String pwd;

    @Column(name = "description", length = 100, nullable = false)
    private String description;

    @Column(name = "level", nullable = false)
    private int level;

    @Column(name = "avator", length = 100, nullable = true)
    private String avator;

    @Column(name = "address", length = 100, nullable = true)
    private String address;
    @Column(name = "phone", length = 30, nullable = true, unique = true)
    private String phone;
    @Column(name = "email", length = 100, nullable = true, unique = true)
    private String email;

    @Column(name = "enabled")
    private boolean enabled;

    //引用的权限实体对象集合
    @Lazy(true)
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "SysUserRole",
    joinColumns = {@JoinColumn(name = "userId", referencedColumnName = "id")},
    inverseJoinColumns = {@JoinColumn(name="roleId", referencedColumnName = "id")})
    private List<SysRole> roles;

    public static SysUser ofRegister(String name, String pwd, String desc, int level){
        return new SysUser(0L, name, pwd, desc, level, "", "", "", "", true, null);
    }

}
